﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

/// <summary>
///
/// 全局定义设置类
/// 
/// ----------------------------------------
/// 作者：hep
/// 创建时间：2022-7-29 17:44:03
/// ----------------------------------------
/// </summary>
public class GlobalDefineEditor : EditorWindow, IHasCustomMenu
{


    static List<string> currentDefineList = null;
    static Vector2 scroll;



    //平台
    static readonly BuildTargetGroup[] buildTargetGroups = new BuildTargetGroup[]
    {
        BuildTargetGroup.Android,
        BuildTargetGroup.iOS,
        BuildTargetGroup.Standalone,
        BuildTargetGroup.WebGL,
    };



    /// <summary>
    /// 定义面板展示
    /// </summary>
    static readonly Dictionary<string, string> defineSymbolDictionary = new Dictionary<string, string>
    {
        {"DEBUG_SHOW","Debug展示"},
        {"DEBUG_MODE", "研发模式"},
        {"TEST_MODE","测试环境"},
        {"RELEASE_MODE","正式模式"},
    };





    /// <summary>
    /// 打开脚本的方法
    /// </summary>
    /// <param name="_menu"></param>
    public void AddItemsToMenu(GenericMenu _menu)
    {
        //附加内容添加
        _menu.AddItem(new GUIContent("Open Script"), false, () =>
        {
            TextAsset obj = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Scripts/HEFramework/Editor/GlobalDefineEditor.cs");
            if (obj != null)
            {
                Selection.activeObject = obj;
                AssetDatabase.OpenAsset(obj, 0);
            }
        });
    }


    /// <summary>
    /// Unity菜单选项附加
    /// </summary>
    [MenuItem("MyTools/GlobalDefine", false, 0)]
    static void Open()
    {
        GlobalDefineEditor window = GetWindow<GlobalDefineEditor>("Global Define");
        window.Focus();
        window.Show(true);
    }



    /// <summary>
    /// 给界面划横线
    /// </summary>
    void DrawLine()
    {
        GUILayout.Box("", GUILayout.Height(2F), GUILayout.ExpandWidth(true));
    }




    /// <summary>
    /// 描画
    /// </summary>
    void OnGUI()
    {
        currentDefineList = GetGlobalDefine();
        DrawLine();

        //页面风格表示
        GUIStyle style = new GUIStyle();
        style.fontSize = 16;
        style.contentOffset = new Vector2(10F, 0F);
        style.normal.textColor = Color.white;
        GUILayout.Label("Edit Global Define", style);
        GUILayout.Space(6f);

        GUILayout.BeginHorizontal();
        GUILayout.Label("If you want to add new define, look at \"GlobalDefineEditor.cs\"");

        if (GUILayout.Button("Clear All"))
        {
            System.Array.ForEach<BuildTargetGroup>(buildTargetGroups, target => PlayerSettings.SetScriptingDefineSymbolsForGroup(target, ""));
        }
        GUILayout.EndHorizontal();

        GUILayout.Space(10F);

        scroll = GUILayout.BeginScrollView(scroll);

        foreach (string key in defineSymbolDictionary.Keys)
        {
            DrawLine();

            //描绘每个新的定义
            GUILayout.BeginHorizontal();
            bool enabled = GUILayout.Toggle(currentDefineList.Contains(key), "  " + key, GUILayout.Width(200F));
            GUILayout.Label(defineSymbolDictionary[key]);

            if (enabled != currentDefineList.Contains(key))
            {
                if (enabled)
                {
                    currentDefineList.Add(key);
                }
                else
                {
                    currentDefineList.Remove(key);
                }

                // 更新旧的全局定义
                SetGlobalDefine(currentDefineList);
                currentDefineList = GetGlobalDefine();
            }

            GUILayout.EndHorizontal();
        }

        GUILayout.EndScrollView();
    }



    /// <summary>
    /// 全局定义的获取
    /// </summary>
    /// <returns>PlayerSetting全局定义</returns>
    private static List<string> GetGlobalDefine()
    {
        List<string> aDefineList = new List<string>();
        string aDefineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
        foreach (string def in aDefineSymbols.Split(';'))
        {
            aDefineList.Add(def);
        }
        aDefineList.RemoveAll(def => def == string.Empty || !defineSymbolDictionary.ContainsKey(def));
        return aDefineList;
    }




    /// <summary>
    /// 全局定义的更新
    /// </summary>
    /// <param name="_currentDefineList">新的定义列表</param>
    private static void SetGlobalDefine(List<string> _currentDefineList)
    {
        string aNewDefineSymbols = string.Join(";", _currentDefineList.ToArray());
        System.Array.ForEach<BuildTargetGroup>(buildTargetGroups, target => PlayerSettings.SetScriptingDefineSymbolsForGroup(target, aNewDefineSymbols));
    }



    public static void Clear()
    {
        System.Array.ForEach<BuildTargetGroup>(buildTargetGroups, target => PlayerSettings.SetScriptingDefineSymbolsForGroup(target, string.Empty));
    }

    /// <summary>
    /// 全局定义动态追加
    /// 如果存在则无视
    /// </summary>
    /// <param name="_define">动态申请的定义</param>
    public static void AddDefine(string _define)
    {
        List<string> define_list = GetGlobalDefine();
        if (!define_list.Contains(_define))
        {
            define_list.Add(_define);
        }

        SetGlobalDefine(define_list);
    }

}